package com.polaris.live.adapter.message

import android.view.View
import androidx.core.content.ContextCompat
import com.polaris.live.R
import com.polaris.live.common.R.color
import com.polaris.live.common.adapter.BaseBindingAdapter
import com.polaris.live.common.adapter.VBViewHolder
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.databinding.AdapterWithdrawDetailBinding
import com.polaris.live.manager.AppConfigManager
import com.polaris.live.resp.back_resp.WithdrawAccountInfoVo
import com.polaris.live.translation.R.string
import com.polaris.live.utils.CommonUtils.toBoolean

/**
 * WithdrawDetailAdapter
 *
 * @author Created by 长空 on 2023/10/26/014 13:55
 */
class WithdrawDetailAdapter :
    BaseBindingAdapter<AdapterWithdrawDetailBinding, WithdrawAccountInfoVo>() {

    init {
        addChildClickViewIds(R.id.iv_delete, R.id.iv_edit_user)
    }

    private val isWithdrawalEdit =
        AppConfigManager.getConfig(CommonConst.Configs.withdrawalEdit) ?: "0"

    private var selectPosition = -1

    override fun convert(
        holder: VBViewHolder<AdapterWithdrawDetailBinding>,
        item: WithdrawAccountInfoVo,
    ) {
        val position = holder.absoluteAdapterPosition
        holder.vb.apply {
            checkbox.isChecked = position == selectPosition
            tvBank.text = item.channelName
            tvFreeCard.text = item.accountNum
            tvFee.text = context.getString(string.commission_charge, item.fee)
            if (item.status == 0) {
                ivEditUser.visibility = View.GONE
                tvLose.visibility = View.VISIBLE
                tvBank.setTextColor(
                    ContextCompat.getColor(
                        context,
                        color.color_a7a8ad
                    )
                )
                tvFreeCard.setTextColor(
                    ContextCompat.getColor(
                        context,
                        color.color_a7a8ad
                    )
                )
            } else {
                if (isWithdrawalEdit.toInt().toBoolean()) {
                    ivEditUser.visibility = View.VISIBLE
                } else {
                    ivEditUser.visibility = View.GONE
                }

                tvLose.visibility = View.GONE
                tvBank.setTextColor(
                    ContextCompat.getColor(
                        context,
                        color.color_202028
                    )
                )
                tvFreeCard.setTextColor(
                    ContextCompat.getColor(
                        context,
                        color.color_202028
                    )
                )
            }
        }
    }

    fun setSelect(position: Int) {
        selectPosition = position
        notifyDataSetChanged()
    }

    fun clearSelected() {
        if (selectPosition == -1) {
            return
        }
        val index = selectPosition
        selectPosition = -1
        notifyItemChanged(index)
    }

}